var express = require('express');
var router = express.Router();
let { isUser, registerUser, isLogin, getOneType, getTwoType, getGoodsByTwo, getGoodsById, submitOrder, getGoodsByName } = require('../mysql/index');
router.post('/loginregister', async (req, res, next) => {
    let val = req.body;
    let jsonData = null;
    // const sessionCaptcha = req.session.captcha.toLowerCase();
    // if (sessionCaptcha != val.captcha) {
    //   return res.json({
    //     flog: 0,
    //     data: '验证码不正确'
    //   });
    // }
    let data = await isUser({
        username: val.username
    })
    if (data.length) {
        jsonData = {
            code: 0,
            data: '该用户名已存在'
        }
    } else {
        let info = await registerUser({
            username: val.username,
            password: val.password
        })
        if (info) {
            jsonData = {
                code: 1,
                data: data
            }
            req.session.UserInfo = {
                username: val.username,
                id: info.insertId
            }
        } else {
            jsonData = {
                code: 0,
                data: "注册失败"
            }
        }
    }
    return res.json(jsonData)
});
router.post('/islogin', async (req, res, next) => {
    let { username, password } = req.body;
    let jsonData;
    let data = await isLogin({
        username, password
    })
    if (!data.length) {
        jsonData = {
            code: 0,
            data: "账号或者密码错误"
        }
    } else {
        jsonData = {
            code: 1,
            data: data
        }
        req.session.UserInfo = {
            username: username,
            id: data[0].id
        }
    }
    return res.json(jsonData)
});
router.get('/goodsType', async (req, res, next) => {
    let oneTypeList = await getOneType()
    let twoTypeList = await getTwoType()
    let typeList = []
    oneTypeList.forEach(oneType => {
        oneType.twoTypeList = []
        twoTypeList.forEach(twoType => {
            if (twoType.oneTypeId == oneType.oneTypeId) {
                oneType.twoTypeList.push(twoType)
            }
        })
        typeList.push(oneType)
    })
    return res.json({
        code: 1,
        typeList: typeList
    })
})
router.post('/getGoodsByTwo', async (req, res, next) => {
    let { twoId } = req.body
    let goodsList = await getGoodsByTwo(twoId)
    goodsList.forEach(item => {
        item.goodsImgList = JSON.parse(item.goodsImgList)
    })
    return res.json({
        code: 1,
        goodsList: goodsList
    })
})
router.post('/getGoodsById', async (req, res, next) => {
    let { goodsId } = req.body;
    let data = await getGoodsById(goodsId);
    data.goodsImgList = JSON.parse(data.goodsImgList);
    res.json({
        code: 1,
        data
    })
})
router.post('/submitOrder', async (req, res, next) => {
    let { orderData, allAmt, allNum, userId } = req.body
    userId = req.session.UserInfo.id
    await submitOrder({
        orderData, allAmt, allNum, userId
    })
    return res.json({
        code: 1,
        msg: '提交成功'
    })
})
router.post('/getGoodsByName', async (req, res, next) => {
    let { goodsName } = req.body
    let goodsList = await getGoodsByName({
        goodsName
    })
    if (goodsList.length > 0) {
        goodsList.forEach(item => {
            item.goodsImgList = JSON.parse(item.goodsImgList)
        })
    }
    return res.json({
        code: 1,
        goodsList: goodsList
    })
})
module.exports = router;
